import CanvasApi from "@/api/canvasApi";
import PublicEvent from "@/assets/nodeIcon/publicEvent.png";
import { conditionalOriginal } from "@/utils"
import {Ref} from "vue";

export interface IPublicEventItem {
  showInCanvas: number,
  name: string,
  icon: string,
  propertyKey: string,
  propertyType: string,
}

export const useGetPublicEvent = () => {
  const store = useStore()
  const publicEventList: Ref<IPublicEventItem[]> = ref<IPublicEventItem[]>([])

  const getPublicEvent = async () => {
    const rs = await CanvasApi.geCanvasPublicEventEntity({
      showCount: -1,
      currentPage: 1,
    });

    if (rs.code === 200) {
      publicEventList.value = rs.payload
        .filter((item: IPublicEventItem) => item.showInCanvas === 1)
        .map((item: IPublicEventItem) => ({
          key: 'event',
          label: item.name,
          iconSrc: PublicEvent,
          data: {
            dialogData: {
              ...item,
              conditionalOriginal: JSON.stringify(conditionalOriginal(item, 'event')),
            }
          }
        }))

      store.commit("event/setPublicEvent", rs.payload.map((item: IPublicEventItem) => ({
        propertyKey: item.propertyKey,
        propertyName: item.name,
        propertyType: item.propertyType,
      })));
    }
  }

  return {
    publicEventList,
    getPublicEvent,
  }
}
